$(function() {
	var tableInstance = $('#example').DataTable({
		"dom": '<"top">rt<"bottom"ip><"clear">',
		"data": [],
		"ordering": false,
		"columns": [
			{
				"data": "type",
				"render": function(data, type, full, meta) {
					if(data == 2) {
						data = "../assets/img/video.png"
					} else if(data == 1) {
						data = "../assets/img/pdf.png"
					}
					return "<img src='" + data + "' />";
				}
			}, 
			{
				"data": "videoName",
				"render": function(data, type, full, meta) {
					if(full.type === 1){
						return full.pdfPath;
					}else{
						return data;
					}
				},
			},
			{
				"render": function(data, type, full, meta) {
					//type=1:pdf文件
					if(full.type === 1){
						full.pdfPath = '%E9%99%B6%E7%93%B7%E7%9A%84%E4%BF%9D%E5%85%BB.pdf';//test
						data = "<a href='showPdf.html?file=" + encodeURIComponent(full.pdfPath) +"' target='_blank' class='am-btn am-btn-default purple'>查看</a>"
								+ '<button class="am-btn am-btn-default purple">修改<input type="file"></button>';
					}else{
						data = '<button class="am-btn am-btn-default purple" onclick="openPlayModal(\''+full.videoFileId+'\')">查看</button>'
								+ '<button class="am-btn am-btn-default purple" onclick="openUploaModal(this,\''+full.id+'\')">修改</button>';
					}
					return data;
				}
			},
		]
	});
	//获取产品组数据
	Server.getFromCommon("/getProduct", {key:"group"}, function(res){
		if(res.code === 200){
			$.each(res.data, function(i, data){
				$('#productGroup').append('<option value="'+ data.id +'">'+ data.name+'</option>');
			});
		}
	});
	//产品组下拉框value更改事件
	$('#productGroup').change(function(e){
		$('#productSelect').html('<option value="">请选择产品</option>');
		if(this.value){
			Server.getFromManage("/getProduct", {id: this.value}, function(res){
				if(res.code === 200){
					$.each(res.data, function(i, data){
						$('#productSelect').append('<option value="'+ data.id +'">'+ data.serialCode+'</option>');
					});
				}
			});
		}
	});
	//产品下拉框value更改事件
	$('#productSelect').change(function(e){
		Server.getFromManage('/getInstallInfo', {id: this.value}, function(res){
			tableInstance.clear();
			if(res.code === 200){
				//遍历数据，添加表格行
				$.each(res.data, function(i, data){
					tableInstance.row.add(data).draw();
				});
			}
		});
	});
	$('#video-file').on("change", function(e){
		var files = e.target.files[0],url = window.URL || window.webkitURL || window.mozURL;
		if(files){
			console.log(url.createObjectURL(files));
			$('#prev-video').attr('src', url.createObjectURL(files));
		}
	});
});

//记住TCPlayer播放实例，用于切换fileId视频
var player;
/**
 * 打开modal框，播放视频
 * @param {String} fileId 视频fileid 
 */
function openPlayModal(fileId){
	//如果播放视频实例不存在，则初始化
	if(player == undefined){
		player = TCPlayer("id_video_container", {
			fileID: fileId, // 请传入需要播放的视频filID 必须
			appID: "1256187897", // 请传入点播账号的appID 必须
			autoplay: false, //是否自动播放
			"width" :  '520',//视频的显示宽度，请尽量使用视频分辨率宽度
			"height" : '320'
		});
	}else{
		//通过播放实例，切换 fileID 播放
		player.loadVideoByID({
			fileID: fileId, // 请传入需要播放的视频 filID 必须
			appID: "1256187897" // 请传入点播账号的 appID 必须
		});
	}
	//打开modal时，先隐藏内容，延时显示（切换视频，会有延时）
	$('.al-modal-content').hide();
	//opened.modal.amui:Modal 窗口打开完成以后触发（CSS 动画执行完成）
	$('#video-modal').modal('toggle').on('opened.modal.amui', function(){
		$('.al-modal-loding').show();
		setTimeout(() => {
			$('.al-modal-content').show();
			$('.al-modal-loding').hide();
		}, 100);
	});
}
/**
 * 打开修改弹窗
 * @param {Dom} elem 
 * @param {String} key 
 */
function openUploaModal(elem,key){
	$(elem).addClass("btn-active");
	$('#video-upload-modal').modal({
		width: 480,
		height: 320
	});
	$('#video-id').val(key);
}
/**
 * 上传视频
 */
function uploadVideo(){
	if($('#video-id').val() == '' || $('#video-file').val() == ''){
		alert("视频不能为空!");
		return false;
	}
	var file = $('#video-file')[0].files[0];
	var param = {
		file: file,
		finish: function(result){//上传成功时的回调函数
			console.log(result);
			// 判断文件的后缀名,当后缀名 不为mp4,HLS,flv时,进行转码处理
			var extension = file.name.substring(file.name.lastIndexOf(".")+1,file.name.length);
			if (extension != 'mp4' && extension != 'HLS' && extension != 'flv') {
				//将fileId传到后台
				Server.postToFile("/convertVodFile",{fileId:result.fileId},function(result){
				});
			}
			Server.postToManage("/updateInstallFile", {fileId : result.fileId, id: $('#video-id').val()}, function(res){
				if(res.msg === 'not_found'){
					alert("没有找到该视频id");
				}else{
					alert("更新成功");
					$('#video-upload-modal').modal('toggle');
					$('.btn-active').removeClass("btn-active");
				}
			});
		}
	}
	uploadToVod(param);
}